package cn.lingyangwl.agile.msg.push.controller;

import cn.hutool.core.util.RandomUtil;
import cn.lingyangwl.agile.msg.push.api.constatnts.*;
import cn.lingyangwl.agile.msg.push.api.model.content.*;
import cn.lingyangwl.agile.msg.push.api.utils.*;
import cn.lingyangwl.framework.core.response.*;
import cn.lingyangwl.framework.tool.core.*;
import com.alibaba.fastjson2.JSONObject;
import com.github.yitter.idgen.YitIdHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.time.Duration;
import java.util.Date;

/**
 * @author shenguangyang
 */
@Api(tags = "推送测试")
@RestController
@RequestMapping("/msg/push")
public class TestMsgPushController {

    @PostMapping("/testPushEmail")
    public Resp<?> testPushEmail(@RequestBody JSONObject data) {
        String code = data.getString("code");
        String email = data.getString("email");
        MsgParam content = new MsgParam();
        content.setCode(StringUtils.firstNonEmpty(code, "demo"));
        content.setBizKey("1");
        content.setBizType("test");
        content.addMsgField("accountName", "沈光阳")
                .addMsgField("bizTime", DateUtils.getDateOfStr(new Date(), "yyyy-MM-dd HH:mm:ss"))
                .addMsgField("city", "杭州");

        content.addAllMsgField(data);
        MsgPushUtils.pushAsync(ChannelTypeEnum.EMAIL, content, email);
        return Resp.ok();
    }

    @ApiOperation("短信推送")
    @GetMapping("/testPushSms")
    public Resp<?> testPushSms(@RequestParam("phone") String phone) {
        MsgParam content = new MsgParam();
        content.setCode("test_sms");
        content.setBizKey("1").setBizType("test");
        content.addMsgField("value", RandomUtil.randomInt(10000, 99999));
        MsgPushUtils.push(ChannelTypeEnum.SMS, content, phone);
        return Resp.ok();
    }

    @ApiOperation("阿里云短信推送")
    @PostMapping("/testPushAliyunSms")
    public Resp<?> testPushAliyunSms(@RequestBody JSONObject data) {
        String code = data.getString("code");
        String phone = data.getString("phone");
        MsgParam content = new MsgParam();
        content.setCode(StringUtils.firstNonEmpty(code, "demo"));
        content.setBizKey("1").setBizType("test");
        content.addMsgField("value", RandomUtil.randomInt(10000, 99999));
        content.addAllMsgField(data);
        MsgPushUtils.push(ChannelTypeEnum.SMS, content, phone);
        return Resp.ok();
    }

    @ApiOperation("推送微信公众号")
    @PostMapping("/testPushWxMp")
    public Resp<?> testPushWxMp(@RequestBody JSONObject data) {
        MsgParam content = new MsgParam();
        String code = data.getString("code");
        String openId = data.getString("openId");
        content.setCode(StringUtils.firstNonEmpty(code, "demo"));
//        content.setPageUrl("https://www.baidu.com");
        content.addUrlParam("k1", "t");
        content.setBizKey("1").setBizType("test");
        content.addMsgField("goodsName", "华为mate40pro")
                .addMsgField("bizTime", "2020-10-25")
                .addMsgField("goodsPrice", "6999", "#ef5777")
                .addMsgField("remark", "麒麟9000牛逼");

        content.addAllMsgField(data);

        content.pushAfter(Duration.ofSeconds(15));
        MsgPushUtils.pushAsync(ChannelTypeEnum.WX_MP, content, StringUtils.firstNonEmpty(openId, "o63YF52s67WW4SQ8_VkWqVwamN-4"));
        return Resp.ok();
    }

    @ApiOperation("微信小程序服务推送")
    @PostMapping("/testPushWxMa")
    public Resp<?> testPushWxMa(@RequestBody JSONObject data) {
        MsgParam content = new MsgParam();
        String code = data.getString("code");
        String openId = data.getString("openId");
        content.setCode(StringUtils.firstNonEmpty(code, "demo"));
        content.setPageUrl("https://www.baidu.com").setBizKey("1").setBizType("test");
        content.addMsgField("time4", DateUtils.getDateOfStr(new Date(), "yyyy-MM-dd HH:mm:ss"))
                .addMsgField("thing2", "A1098")
                .addMsgField("thing3", "前面还有5桌，请耐心等待")
                .addMsgField("thing1", "麦当劳下渡路餐厅");

        content.addAllMsgField(data);
        content.pushAfter(Duration.ofSeconds(5));
        MsgPushUtils.pushAsync(ChannelTypeEnum.WX_MINI_APP, content, openId);
        return Resp.ok();
    }

    @ApiOperation("pc推送")
    @GetMapping("/testPushPC")
    public Resp<?> testPushPC() {
        MsgParam content = new MsgParam();
        content.setCode("demo");
        content.setBizKey("1").setBizType("test");
        content.addMsgField("orderId", YitIdHelper.nextId());

        MsgPushUtils.push(ChannelTypeEnum.PC, content, "1"); // 用户id
//        MsgPushUtils.push(ChannelTypeEnum.PC, content, "*"); // 给所有用户推送
        return Resp.ok();
    }

    @ApiOperation("推送多通道测试")
    @PostMapping("/pushMultiChannel")
    public Resp<?> pushMultiChannel(@RequestBody JSONObject data) {
        MsgParam content = new MsgParam();
        String code = data.getString("code");
        String email = data.getString("email");
        String phone = data.getString("phone");
        String openId = data.getString("openId");
        content.setCode(StringUtils.firstNonEmpty(code, "demo"));

        content.setTitle("订单支付成功");
        content.setBizKey("1");
        content.setBizType("test");
        content.addMsgField("username", "沈光阳")
                .addMsgField("operateDescribe", "登录")
                .addMsgField("verifyCode", RandomUtil.randomInt(10000, 99999));

        content.addMsgField("goodsName", "华为mate40pro")
                .addMsgField("time", "2020-10-25")
                .addMsgField("price", "6999", "#ef5777")
                .addMsgField("remark", "麒麟9000牛逼");

        content.addAllMsgField(data);

        // 推送邮件
        MsgPushUtils.push(ChannelTypeEnum.EMAIL, content, email);

        // 推送公众号
        MsgPushUtils.push(ChannelTypeEnum.WX_MP, content, openId);

        // 推送短信
        MsgPushUtils.push(ChannelTypeEnum.SMS, content, phone);
        return Resp.ok();
    }


    /**
     * 测试任务接收者类型为 通知管理员 的消息推送
     * 由于任务接收者类型为通知管理员, 所里这里配置的手机号/邮箱/微信openId无效, 由页面绑定的接收者来接收消息
     */
    @PostMapping("/testPushEmailToAdmin")
    public Resp<?> testPushEmailToAdmin(@RequestBody JSONObject data) {
        MsgParam content = new MsgParam();
        String code = data.getString("code");
        content.setCode(StringUtils.firstNonEmpty(code, "demo_to_admin"));
        content.setTitle("这是一个测试标题");
        content.setBizKey("1");
        content.setBizType("test");
        content.addMsgField("data", RandomUtil.randomInt(10000, 99999));
        content.addAllMsgField(data);

        MsgPushUtils.push(ChannelTypeEnum.EMAIL, content, "");
        return Resp.ok();
    }

    @ApiOperation("阿里云短信推送")
    @PostMapping("/testPushAliyunSmsToAdmin")
    public Resp<?> testPushAliyunSmsToAdmin(@RequestBody JSONObject data) {
        MsgParam content = new MsgParam();
        String code = data.getString("code");
        content.setCode(StringUtils.firstNonEmpty(code, "demo_to_admin"));
        content.setBizKey("1").setBizType("test");
        content.addMsgField(MsgFieldEnum.CAPTCHA, RandomUtil.randomInt(10000, 99999));

        content.addAllMsgField(data);
        MsgPushUtils.push(ChannelTypeEnum.SMS, content, null);
        return Resp.ok();
    }

    @ApiOperation("测试微信公众号推送测试")
    @PostMapping("/testPushWxMpToAdmin")
    public Resp<?> testPushWxMpToAdmin(@RequestBody JSONObject data) {
        MsgParam content = new MsgParam();
        String code = data.getString("code");
        content.setCode(StringUtils.firstNonEmpty(code, "demo_to_admin"));
//        content.setPageUrl("https://www.baidu.com")
        content.setBizKey("1").setBizType("test");
        content.addMsgField("goodsName", "华为mate60pro 遥遥领先")
                .addMsgField("time", DateUtils.getDateOfStr(new Date(), "yyyy-MM-dd"))
                .addMsgField("price", "6999", "#ef5777")
                .addMsgField("remark", "麒麟9000牛逼");

        content.addAllMsgField(data);

        content.pushAfter(Duration.ofSeconds(15));
        MsgPushUtils.pushAsync(ChannelTypeEnum.WX_MP, content, "aaa");
        return Resp.ok();
    }

}
